{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Creating a deployment\n",
    "\n",
    "Let's create a deployment of the `task_maistro` app that we created in module 5.\n",
    "\n",
    "## Code structure\n",
    "\n",
    "[The following information should be provided](https://langchain-ai.github.io/langgraph/concepts/application_structure/) to create a LangGraph Platform deployment:\n",
    "\n",
    "* A [LangGraph API Configuration file](https://langchain-ai.github.io/langgraph/concepts/application_structure/#configuration-file) - `langgraph.json`\n",
    "* The graphs that implement the logic of the application - e.g., `task_maistro.py`\n",
    "* A file that specifies dependencies required to run the application - `requirements.txt`\n",
    "* Supply environment variables needed for the application to run - `.env` or `docker-compose.yml`\n",
    "\n",
    "We have this already in the `module-6/deployment` directory! \n",
    "\n",
    "## CLI\n",
    "\n",
    "The [LangGraph CLI](https://langchain-ai.github.io/langgraph/concepts/langgraph_cli/) is a command-line interface for creating a LangGraph Platform deployment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%capture --no-stderr\n",
    "%pip install -U langgraph-cli"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To create a [self-hosted deployment](https://langchain-ai.github.io/langgraph/how-tos/deploy-self-hosted/#how-to-do-a-self-hosted-deployment-of-langgraph), we'll follow a few steps. \n",
    "\n",
    "### Build Docker Image for LangGraph Server\n",
    "\n",
    "We first use the langgraph CLI to create a Docker image for the [LangGraph Server](https://docs.google.com/presentation/d/18MwIaNR2m4Oba6roK_2VQcBE_8Jq_SI7VHTXJdl7raU/edit#slide=id.g313fb160676_0_32).\n",
    "\n",
    "This will package our graph and dependencies into a Docker image.\n",
    "\n",
    "A Docker image is a template for a Docker container that contains the code and dependencies required to run the application.\n",
    "\n",
    "Ensure that [Docker](https://docs.docker.com/engine/install/) is installed and then run the following command to create the Docker image, `my-image`:\n",
    "\n",
    "```\n",
    "$ cd module-6/deployment\n",
    "$ langgraph build -t my-image\n",
    "```\n",
    "\n",
    "### Set Up Redis and PostgreSQL\n",
    "\n",
    "If you already have Redis and PostgreSQL running (e.g., locally or on other servers), then create and run the LangGraph Server container [by itself](https://langchain-ai.github.io/langgraph/how-tos/deploy-self-hosted/#running-the-application-locally) with the URIs for Redis and PostgreSQL:\n",
    "\n",
    "```\n",
    "docker run \\\n",
    "    --env-file .env \\\n",
    "    -p 8123:8000 \\\n",
    "    -e REDIS_URI=\"foo\" \\\n",
    "    -e DATABASE_URI=\"bar\" \\\n",
    "    -e LANGSMITH_API_KEY=\"baz\" \\\n",
    "    my-image\n",
    "```\n",
    "\n",
    "Alternatively, you can use the provided `docker-compose.yml` file to create three separate containers based on the services defined: \n",
    "\n",
    "* `langgraph-redis`: Creates a new container using the official Redis image.\n",
    "* `langgraph-postgres`: Creates a new container using the official Postgres image.\n",
    "* `langgraph-api`: Creates a new container using your pre-built image.\n",
    "\n",
    "Simply copy the `docker-compose-example.yml` and add the following environment variables to run the deployed `task_maistro` app:\n",
    "\n",
    "* `IMAGE_NAME` (e.g., `my-image`) \n",
    "* `LANGCHAIN_API_KEY`\n",
    "* `OPENAI_API_KEY`\n",
    "\n",
    "Then, [launch the deployment](https://langchain-ai.github.io/langgraph/how-tos/deploy-self-hosted/#using-docker-compose):\n",
    "\n",
    "```\n",
    "$ cd module-6/deployment\n",
    "$ docker compose up\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
